package com.game.modular.game.match.service.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.modular.game.playerMatch.mapper.PvpMatchMapper;
import com.game.modular.game.playerMatch.domain.PvpMatch;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;


/**
 * 基础服务-pvp对局信息
 *
 * @author cxy
 * @since 2024-09-11 19:39:58
 */
@Slf4j
@Service
public class PvpMatchBaseServiceImpl extends ServiceImpl<PvpMatchMapper, PvpMatch> {
    private static final String cacheKey = "pvpMatch:%d:match";
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private ValueOperations<Object, PvpMatch> valueOperations;
    @Resource
    private HashOperations<Object, Object, PvpMatch> hashOperations;
    @Resource
    private PvpMatchMapper pvpMatchMapper;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  基础查询
    public List<PvpMatch> list(Integer mapId, Integer type, Integer startThreshold) {
        List<PvpMatch> pvpMatches = pvpMatchMapper.selectList(new LambdaQueryWrapper<PvpMatch>()
                .eq(PvpMatch::getPvpMapId, mapId)
                .eq(PvpMatch::getType, type)
                .eq(PvpMatch::getStatus, 0)
                .eq(PvpMatch::getStartThreshold, startThreshold));
        return pvpMatches;
    }
    public PvpMatch select(Long matchId) {
        PvpMatch pvpMatch = selectCache(matchId);
        if (pvpMatch!=null){
            pvpMatch =  pvpMatchMapper.selectOne(new LambdaQueryWrapper<PvpMatch>().eq(PvpMatch::getId, matchId));
            savaCache(pvpMatch);
        }
        return pvpMatch;
    }
    public PvpMatch getById(Long currentMatchId) {
        PvpMatch pvpMatch = selectCache(currentMatchId);
        if (pvpMatch==null){
            pvpMatch = super.getById(currentMatchId);
        }
        return pvpMatch;
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  更新插入
    @Override
    public boolean updateById(PvpMatch entity) {
        boolean b = super.updateById(entity);
        PvpMatch byId = super.getById(entity.getId());
        savaCache(byId);
        return b;
    }

    @Override
    public boolean save(PvpMatch entity) {
        boolean save = super.save(entity);
        savaCache(entity);
        return save;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  缓存部分
    public void savaCache(Map<String, PvpMatch> map) {
        valueOperations.multiSet(map);
        map.forEach((key, value)->redisTemplate.expire(key, 60, TimeUnit.MINUTES));
    }
    public void savaCache(PvpMatch entity) {
        String key1 = String.format(cacheKey, entity.getId());
        valueOperations.set(key1,entity,60, TimeUnit.MINUTES);
    }
    public PvpMatch selectCache(
            @NotNull(message = "keyValue1不能为空") Long keyValue1
    ) {
        String key1 = String.format(cacheKey, keyValue1);
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
        return valueOperations.get(key1);
    }
    public void removeCache(Long matchId) {
        String key1 = String.format(cacheKey, matchId);
        redisTemplate.delete(key1);
    }
}
